/**
 * This file is part of Adaptools.
 * 
 * Adaptools is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Adaptools is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 * along with Adaptools.  If not, see http://www.gnu.org/licenses/. 
 */
package org.adaptools.repository.base;

import java.util.ArrayList;
import java.util.Collection;

import org.adaptools.base.Mechanism;
import org.adaptools.base.context.AdaptoolsContextFactory;
import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;

/**
 * Repository Base to Adaptools
 * 
 * @author Denys G. Santos (gsdenys@gmail.com)
 * @since 3.0.0
 */
public class RepositoryBase implements Repository {
	private static final Logger LOG = Logger.getLogger(RepositoryBase.class);
	
	private Collection<Mechanism> collection = new ArrayList<Mechanism>(); 
	
	private String helperSting = null;
	
	private String semanticScript = null;
	
	/**
	 * Default Builder
	 */
	public RepositoryBase() {
		LOG.debug("Create a new Repository");
	}

	/**
	 * @see org.adaptools.repository.base.Repository#addMechanism(org.adaptools.base.Mechanism)
	 */
	@Override
	public void addMechanism(Mechanism mechanism) {
		this.collection.add(mechanism);
	}

	/**
	 * @see org.adaptools.repository.base.Repository#remove(java.lang.String)
	 */
	@Override
	public boolean remove(String uuid) {
		ApplicationContext context = AdaptoolsContextFactory.getContext();
			
		Mechanism mechanism = (Mechanism) context.getBean("adaptools.repository.mechanism");
		mechanism.setId(uuid);
		
		return this.collection.remove(mechanism);
	}

	/**
	 * @see org.adaptools.repository.base.Repository#get(java.lang.String)
	 */
	@Override
	public Mechanism get(String uuid) {
		ApplicationContext context = AdaptoolsContextFactory.getContext();
		
		Mechanism mechanism = (Mechanism) context.getBean("adaptools.repository.mechanism");
		mechanism.setId(uuid);
		
		for (Mechanism mech : this.collection) {
			if(mech.equals(mechanism)){
				return mech;
			}
		}
		return null;
	}

	/**
	 * @see org.adaptools.repository.base.Repository#numberOfMechanisms()
	 */
	@Override
	public Integer numberOfMechanisms() {
		return this.collection.size();
	}

	/**
	 * @see org.adaptools.repository.base.Repository#getHelper()
	 */
	@Override
	public String getHelper() {
		return this.helperSting;
	}

	/**
	 * @see org.adaptools.repository.base.Repository#setHelper(String)
	 */
	@Override
	public void setHelper(String helperString) {
		this.helperSting = helperString;
	}
	
	/**
	 * @see org.adaptools.repository.base.Repository#getSemantic()
	 */
	@Override
	public String getSemantic() {
		return this.semanticScript;
	}

	/**
	 * @see org.adaptools.repository.base.Repository#setSemantic(String)
	 */
	@Override
	public void setSemantic(String sString) {
		this.semanticScript = sString;
	}

	/**
	 * @see org.adaptools.repository.base.Repository#getAllMechanism()
	 */
	@Override
	public Collection<Mechanism> getAllMechanism() {
		return this.collection;
	}

}